package com.sb.registration.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.sb.registration.component.model.UserRegistrationServiceResult;
import com.sb.registration.component.model.UserWrapper;
import com.sb.registration.component.service.UserRegistrationManager;

/**
 * Spring MVC controller to provide sample page for user registration. This is provided just for easy access and 
 * testing of user registration service provided as a solution to coding challenge
 * 
 * @author gautam.amitkumar@gmail.com
 *
 */
@Controller
public class UserRegistrationController {
	
	private static Log logger_ = LogFactory.getLog(UserRegistrationController.class);
	
	@Autowired
	private UserRegistrationManager userRegistrationManager;
	
	/**
	 * Request handling method to show default registered users page with list of 
	 * all existing users in the datastore
	 * 
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping(value = "/registeredusers", method = RequestMethod.GET)
	public ModelAndView handleRequest() {
		
		ModelAndView mv = new ModelAndView("register/registeredusers");
		
		UserRegistrationServiceResult result = userRegistrationManager.fetchAllUsers();
		
		if(result.getStatus().equals(UserRegistrationServiceResult.SUCCESS)){
			mv.addObject("userList", result.getUserList());
		}else{
			mv.addObject("error", result.getErrorDetails().getErrorCode() + ", " + result.getErrorDetails().getErrorMessage());
			logger_.error(result.getErrorDetails().getExceptionMessage());
		}
		
		return mv;
	}
	
	/**
	 * Request method to allow add/register new user using user registration service 
	 * All validations are delegated to the service 
	 * 
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping(value = "/register/new", method = RequestMethod.POST)
	public ModelAndView registerNewUser(UserWrapper userData, BindingResult errors, HttpServletRequest request,
            HttpServletResponse response) {

		ModelAndView mv = new ModelAndView("register/registeredusers");
			
		UserRegistrationServiceResult result = userRegistrationManager.addUser(userData);			
	
		if(result.getStatus().equals(UserRegistrationServiceResult.SUCCESS)){
			mv.addObject("message", "Registered Successfully.");
		}else{
			mv.addObject("error", result.getErrorDetails().getErrorCode() + ", " + result.getErrorDetails().getErrorMessage());
			logger_.error(result.getErrorDetails().getExceptionMessage());
			mv.addObject("userData", userData);
			
		}
		
		// load user list after successful registration of user.
		result = userRegistrationManager.fetchAllUsers();
		
		if(result.getStatus().equals(UserRegistrationServiceResult.SUCCESS)){
			mv.addObject("userList", result.getUserList());
		}else{
			mv.addObject("error", result.getErrorDetails().getErrorCode() + ", " + result.getErrorDetails().getErrorMessage());
			logger_.error(result.getErrorDetails().getExceptionMessage());
		}

		return mv;

	}
	

}
